//=============================================================================
#include "AppStateServer.h"
#include "AppStateManager.h"
#include <sstream>

//=============================================================================
CAppStateServer CAppStateServer::Instance;

//=============================================================================
CAppStateServer::CAppStateServer() {
	
}

//=============================================================================
void CAppStateServer::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
	switch(state)
	{
		case SERVER_MENU:
		{
			break;
		}
		case SERVER_HOST:
		{
			break;
		}
		default:
		{
			break;
		}
	}
}

//------------------------------------------------------------------------------
void CAppStateServer::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) {
	switch(state)
	{
		case SERVER_MENU:
		{
			
			break;
		}
		case SERVER_HOST:
		{
			break;
		}
		default:
		{
			break;
		}
	}
}


void CAppStateServer::OnMouseMove(int mX, int mY, int relX, int relY, bool Left,bool Right,bool Middle)
{
}

//=============================================================================
void CAppStateServer::OnActivate() {
	state = SERVER_HOST;
	serverBG = CSurface::OnLoad("./images/server/ServerBackGround.png");

	CClient::getInstance().DoSomething(false);

	if(CArea::AreaControl.OnLoad("./maps/1.area") == false) {
    	return;
    }

	//Font initialisation
	if( TTF_Init() == -1 )
    {
        return;    
    }
	font = NULL;
	font = TTF_OpenFont("FreeSans.ttf", 18);
	if (font == NULL)
	{
		return;
	}

}

//-----------------------------------------------------------------------------
void CAppStateServer::OnDeactivate() {
	CArea::AreaControl.OnCleanup();

    //--------------------------------------------------------------------------
    // Entities
    //--------------------------------------------------------------------------
    for(int i = 0;i < CEntity::PlayerList.size();i++) {
        if(!CEntity::PlayerList[i]) continue;
		
        CEntity::PlayerList[i]->OnCleanup();
    }

    CEntity::PlayerList.clear();
	
	
	for(int i = 0;i < CEntity::ProjectileList.size();i++) {
	if(!CEntity::ProjectileList[i]) continue;

	CEntity::ProjectileList[i]->OnCleanup();
	}
	CEntity::ProjectileList.clear();
}

//-----------------------------------------------------------------------------
void CAppStateServer::OnLoop() {

	switch(state)
	{
		case SERVER_MENU:
		{
			break;
		}
		case SERVER_HOST:
		{
			// IMPORTANT
			// All changes done here below the RunServer, must also be done in
			// CAppStateGame, below RunClient.

			//--------------------------------------------------------------------------
			// Entities
			//--------------------------------------------------------------------------
			for(int i = 0;i < CEntity::PlayerList.size();i++) {
				if(!CEntity::PlayerList[i]) continue;

				CEntity::PlayerList[i]->OnLoop();
			}

			for(int i = 0;i < CEntity::ProjectileList.size();i++) {
				if(!CEntity::ProjectileList[i]) continue;

				CEntity::ProjectileList[i]->OnLoop();
			}

			//Collision Events
			for(int i = 0;i < CEntityCol::EntityColList.size();i++) {
				CEntity* EntityA = CEntityCol::EntityColList[i].EntityA;
				CEntity* EntityB = CEntityCol::EntityColList[i].EntityB;

				if(EntityA == NULL || EntityB == NULL) continue;

				if(EntityA->playerID == EntityB->playerID) continue;

				if(EntityA->OnCollision(EntityB)) {       
					if(EntityA->Type == 4)
					{
						EntityB->OnCollision(EntityA);		
					}		
					else
					{
						EntityB->OnCollision(EntityA);
					}
				}
		   }

			CEntityCol::EntityColList.clear();
			break;
		}
		default:
		{
			break;
		}
	}
}

//-----------------------------------------------------------------------------
void CAppStateServer::OnRender(SDL_Surface* Surf_Display) {
	
	SDL_BlitSurface(serverBG, NULL, Surf_Display, NULL);
	
	switch(state)
	{
		case SERVER_MENU:
		{
			break;
		}
		case SERVER_HOST:
		{
			break;
		}
		default:
		{
			break;
		}
	}
	
		
}

//=============================================================================
CAppStateServer* CAppStateServer::GetInstance() {
	return &Instance;
}

//=============================================================================
